package com.lanou3g.code0416;

import java.io.*;

/**
 * Created by dllo on 18/4/16.
 */
public class Main {
    public static void main(String[] args) {



        // 内存和硬盘数据交互耗时,减少二者交互次数
      //  BufferedInputStream fis = new BufferedInputStream();
    //  long t = System.currentTimeMillis();// ms
//        String srcPath = "C:\\Users\\73448\\Desktop\\aaa.mp4";
//        String desPath = "C:\\Users\\73448\\Desktop\\bbb.mp4";
        String srcPath = "p.txt";
        String desPath = "a.txt";
        String desPath1 = "C:\\Users\\73448\\Desktop\\ccc.mp4";
        long startTime = System.currentTimeMillis();
       // copyByBuffer(srcPath, desPath);
        long midTime = System.currentTimeMillis();
        System.out.println("->>" + (midTime - startTime) + "ms");
//        copyByFile(srcPath, desPath1);
//        long endTime = System.currentTimeMillis();
//        System.out.println("<<-" + (endTime - midTime) + "ms");

    }

    public static void copyByFile(String srcPath,String desPath){

        File scrFile = new File(srcPath);
        File desFile = new File(desPath);

        FileInputStream fis = null;
        FileOutputStream fos = null;


        try {
            desFile.createNewFile();

            fis = new FileInputStream(scrFile);
            fos = new FileOutputStream(desFile);

            int pos = 0;
           // byte[] buf = new byte[1024 * 10];   //大小影响两者速度   越大差距越小  一滴一滴的用file更快

            int EOF = -1; // end of file
            while ((pos = fis.read()) != EOF){
                fos.write(pos);    //一滴一滴的传 (二者都改)
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            //会在bos和bis的close方法中自动关闭fis和fos
            IOUtils.closeAll(fis,fos);
        }

    }




    public static void copyByBuffer(String srcPath,String desPath){

        File scrFile = new File(srcPath);
        File desFile = new File(desPath);

        FileInputStream fis = null;
        FileOutputStream fos = null;

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;


        try {
            desFile.createNewFile();

            fis = new FileInputStream(scrFile);
            fos = new FileOutputStream(desFile);

            //BufferedInput/OutputStream 创建的时候需要有别的字节流
            bis = new BufferedInputStream(fis);  //在内部fis已经关闭过一次了
            bos = new BufferedOutputStream(fos);


            int pos = 0;
            byte[] buf = new byte[1024];

            int sum = 0;
            int index = 0;  // 拆掉多个部分,但是不能正常打开    需拼接后正常读
            int EOF = -1; // end of file
            while ((pos = bis.read(buf)) != EOF){
                bos.write(buf,0,pos);
               // bos.flush(); // 放到循环,桶就没用了,瓢多大速度就多大
                sum += pos;
                if (sum >= 100*1024*1024){
                    //换输出流
                   String nextPath = desPath+index ;
                    index ++;
                    File nextFile = new File(nextPath);

                    //换之前先关掉之前的
                    bos.close();
                    bos = new BufferedOutputStream(new FileOutputStream(nextFile));
                    sum = 0;
                }
            }
            //在使用ufferedOutPutStream的时候,需要在B最后加上一句 bos.flush();
            //8196数组满了,才会往硬盘上写东西
            //而flush是无论bos里面的数组满没满,都将里面的数据写在硬盘中
            bos.flush(); //防止数据丢失

            //如果写完就关流,bos.close()中自带.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            //会在bos和bis的close方法中自动关闭fis和fos
            IOUtils.closeAll(bis,bos);
        }

    }




}
